﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;
using WatiN.Core.HtmlUnit;
using com.gargoylesoftware.htmlunit;
using com.gargoylesoftware.htmlunit.html;

namespace HUTest
{
	class Program
	{
		static void Main(string[] args)
		{
			HU hu = new HU(HUBrowserType.Firefox);
			//HU hu = new HU(HUBrowserType.Firefox);

			//hu.ToggleJavascript(false);

			hu.GoTo("http://www.a1-webmarks.com/");

			Link link = hu.Link(Find.ByText("Login"));


			link.Click();

			while (!hu.Button(Find.ByName("username")).Exists)
			{
				hu.WaitForComplete(10);
			}

			hu.TextField(Find.ByName("username")).TypeText("tomatosauce");
			hu.TextField(Find.ByName("password")).TypeText("afmoney9119");
			string html = ((HUBrowser)hu.NativeBrowser).CurrentPage.asXml();
			hu.WaitForComplete();

			hu.Button(Find.ByValue("Submit")).Click();
			hu.WaitForComplete();


			html = ((HUBrowser)hu.NativeBrowser).CurrentPage.asXml();
			

			hu.Link(Find.ByText("My Profile")).Click();

			if (hu.ContainsText("Redth"))
				Console.WriteLine("Logged In!");
			else
				Console.WriteLine("No Go");




			//HU hu = new HU();
			//hu.GoTo("https://mail.google.com");

			//TextField emailField = hu.TextField(Find.ByName("Email"));
			//emailField.TypeText("jondick");

			//TextField pwField = hu.TextField(Find.ByName("Passwd"));
			//pwField.TypeText("vla125ty");

			//CheckBox cb = hu.CheckBox(Find.ByName("PersistentCookie"));
			//cb.Checked = true;

			//Button loginButton = hu.Button(Find.ByName("signIn"));
			//loginButton.Click();

			//hu.GoTo("https://mail.google.com/mail/?ui=html&zy=e");

			////Link loadHtml = hu.Link(Find.ByText("Load basic HTML"));
			////loadHtml.Click();

			//Link composeMail = hu.Link(Find.ByText("Compose&nbsp;Mail"));
			//composeMail.Click();

			//TextField toField = hu.TextField(Find.ByName("to"));
			//toField.TypeText("jondick@gmail.com");

			//TextField subjectField = hu.TextField(Find.ByName("subject"));
			//subjectField.TypeText("Test");

			//TextField bodyField = hu.TextField(Find.ByName("body"));
			//bodyField.TypeText("Testing");
			
			//Button sendButton = hu.Button(Find.ByName("nvp_bu_send"));
			
			//sendButton.Click();

			//if (hu.ContainsText("Sent"))
			//    Console.WriteLine("SUCCESS");

			//for (int i = 0; i < 5; i++)
			//	doit();

			
			
			Console.ReadLine();

		}

		static void doit()
		{
			DateTime start = DateTime.Now;

			WatiN.Core.HU hu = new WatiN.Core.HU();
			hu.GoTo("http://www.google.com");

			TextField tf = hu.TextField(Find.ByName("q"));

			tf.TypeText("Redth");

			string val = tf.Text.ToString();
			hu.Button(Find.ByName("btnG")).Click();

			
			bool found = false;

			if (hu.ContainsText("Twitter / redth"))
				found = true;

			//hu.Frame(Find.by
			DateTime end = DateTime.Now;
			TimeSpan et = start - end;

			Console.WriteLine(string.Format("{0}: {1}s {2}ms {3} ticks", found, et.Seconds, et.Milliseconds, et.Ticks));
		}
	}
}
